package pers.mihao.quickstream.publish.repository.impl;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionTemplate;
import pers.mihao.quickstream.common.exception.BaseException;
import pers.mihao.quickstream.publish.model.convertor.PublishTaskConvertor;
import pers.mihao.quickstream.publish.model.dto.PublishDetailTaskDTO;
import pers.mihao.quickstream.publish.repository.PublishDetailTaskRepository;
import pers.mihao.quickstream.publish.repository.domain.PublishDetailTaskDO;
import pers.mihao.quickstream.publish.repository.domain.PublishTaskDO;
import pers.mihao.quickstream.publish.repository.mapper.PublishDetailTaskMapper;
import pers.mihao.quickstream.publish.repository.mapper.PublishTaskMapper;

import javax.annotation.Resource;

/**
 * @author mihao
 * @since 2024/11/23
 */
@Repository
public class PublishDetailTaskRepositoryImpl implements PublishDetailTaskRepository {

    @Resource
    PublishDetailTaskMapper publishDetailTaskMapper;

    @Resource
    PublishTaskConvertor publishTaskConvertor;

    @Resource
    TransactionTemplate transactionTemplate;


    @Override
    public void create(PublishDetailTaskDTO detailTaskDTO) {
        PublishDetailTaskDO publishDetailTaskDO = publishTaskConvertor
                .map2PublishDetailTaskDO(detailTaskDTO);
        transactionTemplate.execute(c -> {
            int insert = publishDetailTaskMapper.insert(publishDetailTaskDO);
            if (insert != 1) {
                c.setRollbackOnly();
                throw new BaseException();
            }
            return c;
        });
    }

    @Override
    public PublishDetailTaskDTO queryById(long detailTaskId) {
        PublishDetailTaskDO publishDetailTaskDO = publishDetailTaskMapper.selectById(detailTaskId);
        return publishTaskConvertor.map2PublishDetailTaskDTO(publishDetailTaskDO);
    }
}
